//
//  RdbNavigationControllerViewController.swift
//  rdb-led-banner
//
//  Created by radiobox3 on 2025/4/18.
//

import UIKit

class RdbNavigationController: UINavigationController {

    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        self.modalPresentationStyle = .overFullScreen
        self.configBarAppearance()
        /// 监听语言设置变化
        NotificationCenter.default.addObserver(self, selector: #selector(languageChanged), name: .rdbLanguageChange, object: nil)
        
    }
    
    /// 自定义导航栏
    func configBarAppearance(){
        let appearance = UINavigationBarAppearance()
        
 
        if Locale.isRTL() {
            // navigation title to right
            appearance.titlePositionAdjustment = UIOffset(horizontal: 1000, vertical: 1)
            appearance.setBackIndicatorImage(backImage?.rtlImage()
                .withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -6))
                .withRenderingMode(.alwaysOriginal), transitionMaskImage: backImage?.rtlImage())
        } else {
            // navigation title to left
            appearance.titlePositionAdjustment = UIOffset(horizontal: -1000, vertical: 1)
            
            appearance.setBackIndicatorImage(backImage?.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: -6, bottom: 0, right: 0))
                .withRenderingMode(.alwaysOriginal), transitionMaskImage: backImage)
        }
        
        if #available(iOS 14.0, *) {
            UINavigationBar.appearance().items?.forEach({ item in
                item.backButtonDisplayMode = .minimal
            })
        }
        
        /// 设置导航栏标题文字样式
        appearance.titleTextAttributes = [
            NSAttributedString.Key.font: UIFont.medium(20),
            NSAttributedString.Key.foregroundColor: UIColor.white
        ]
        /// 隐藏 back
        appearance.backButtonAppearance.normal.titleTextAttributes = [
            .foregroundColor: UIColor.clear,
            .font: UIFont.systemFont(ofSize: 0.1)
        ]
        /// 去除导航栏的背景效果
        appearance.shadowColor = .clear
        appearance.backgroundEffect = nil
        
        /// 将上述配置应用到导航栏上
        self.navigationBar.standardAppearance = appearance
        self.navigationBar.scrollEdgeAppearance = appearance
        self.navigationBar.compactAppearance = appearance
        if #available(iOS 15.0, *) {
            self.navigationBar.compactScrollEdgeAppearance = appearance
        }
        UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
        UINavigationBar.appearance().shadowImage = UIImage()
    }
    
    
    lazy var backImage: UIImage? = {
        let backImage = UIImage(named: "navigation_back")
        return backImage
    }()
    
    @objc func languageChanged(_ notification: Notification){
        self.configBarAppearance()
    }
    
    override var shouldAutorotate: Bool{
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }

    deinit{
        /// 移除监听
        NotificationCenter.default.removeObserver(self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    

}
